<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Role;
use App\Admin\Auth;
use Input;
class RoleController extends Controller
{
    //角色列表
    public function index(){
    	//获取数据
    	$data = Role::get();
    	return view('admin.role.index',['data' => $data]);
    }

    //分派权限
    public function assignAuth(){
    	if(Input::method() == 'POST'){
    		//获取相应的数据
    		$role_id = Input::get('id');
    		$auth_ids = Input::get('auth');
    		//调用模型，去处理数据
    		$role = new Role();
    		$result = $role -> saveAuth($role_id,$auth_ids);
    		//返回
    		return $result;
    	}else{
    		//获取当前数据表中的权限信息
	    	//分开去获取权限信息
	    	$top = Auth::where('pid','0') -> get();
	    	$sec = Auth::where('pid','>','0') -> get();
	    	$role_id = Input::get('id');//获取角色id
	    	$ids = Role::select('auth_ids') -> where('id',$role_id) -> first();
	    	return view('admin.role.assignAuth',['top' => $top,'sec' => $sec,'ids' => $ids]);
    	}
    }
}
